package com.dm.hdfxxt.warehousemanage.service.impl;

import com.dm.hdfxxt.warehousemanage.mapper.WhMapper;
import com.dm.hdfxxt.warehousemanage.service.WhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class WhServiceImpl implements WhService {
    @Autowired
    private WhMapper whMapper;

    /**
         * @Description 保存仓库信息
         * @Author zhangxue
         * @Date 11:19 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveWh(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        List<Map<String,Object>> GiftList = whMapper.queryWhByCondition(param);
        if(GiftList.size()>0){
            StatusMap.put("Status",-1);//仓库已存在
            return StatusMap;
        }
        int i = whMapper.saveWh(param);
        if(i==1){
            StatusMap.put("Status",1);//保存成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        return StatusMap;
    }

    /**
     * @Description 编辑仓库信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editWh(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = whMapper.editWh(param);
        if(i==1){
            StatusMap.put("Status",1);//编辑成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        List<Map<String,Object>> GiftList = whMapper.queryWhByCondition(param);
        if(GiftList.size()>1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",-1);//仓库已存在
            return StatusMap;
        }
        return StatusMap;
    }

    /**
     * @Description 删除仓库信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delWh(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = whMapper.delWh(param);
        if(i==1){
            StatusMap.put("Status",1);//删除成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//删除失败
        }
        return StatusMap;
    }

    @Override
    public List<Map<String, Object>> queryWhByParm(Map<String, Object> queryWhByParm) {
        List<Map<String,Object>> ResultList = whMapper.queryWhByParm(queryWhByParm);
        return ResultList;
    }

    /**
         * @Description 查询所有仓库
         * @Author zhangxue
         * @Date 11:36 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryAllWh(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = whMapper.queryAllWh(param);
        for(int i=0;i<ResultList.size();i++){
            String CreateTimeStr = ResultList.get(i).get("CreateTime").toString();
            ResultList.get(i).put("CreateTime",CreateTimeStr);
        }
        return ResultList;
    }
}
